<script lang="ts" setup name="XtxDialog">
import { onMounted, ref, watch } from 'vue';
import { onClickOutside } from '@vueuse/core'
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits<{
  (e: 'update:visible', visible: boolean): void
}>()

// 关闭对话框
const closeDialog = () => {
  emit('update:visible', false)
}

const target = ref(null)

onClickOutside(target, () => {
  // 关闭对话框
  closeDialog()
})

const isShow = ref(false)
// 核心: 先渲染, 再去进行切换(setTimeOut), 否则会合并渲染, 无动画效果, 
// onMounted(() => {
//   setTimeout(() => {
//     isShow.value = true
//   }, 200);
// })
// 监听 -> visible 改变 -> 控制isShow的值
watch(() => props.visible, (value) => {
  // console.log('----value', value);
  setTimeout(() => {
    isShow.value = value
  }, 20);
}, {
  immediate: true
})

</script>
<template>
  <!-- 遮罩层 -->
  <div class="xtx-dialog" v-if="visible" :class="{ fade: isShow }">
    <div class="wrapper" ref="target" :class="{ fade: isShow }">
      <!-- 头部 -->
      <div class="header">
        <h3>{{ title }}</h3>
        <!-- 关闭按钮 -->
        <a href="JavaScript:;" @click="closeDialog" class="iconfont icon-close-new"></a>
      </div>
      <!-- 主体 -->
      <div class="body">
        <!-- 默认插槽 -->
        <slot></slot>
      </div>
      <!-- 底部 -->
      <div class="footer">

        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-dialog {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8887;
  // background: rgba(0, 0, 0, 0.5);
  // 纯黑纯透明
  background: rgba(0, 0, 0, 0);
  &.fade {
    transition: all 2s;
    // 纯黑半透明
    background: rgba(0, 0, 0, 0.5);
  }
  .wrapper {
    width: 600px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    // transform: translate(-50%, -50%);

    transform: translate(-50%, -60%);
    opacity: 0;
    &.fade {
      transition: all 2s;
      transform: translate(-50%, -50%);
      opacity: 1;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;

      .icon-warning {
        color: @priceColor;
        margin-right: 3px;
        font-size: 16px;
      }
    }

    .footer {
      text-align: center;
      padding: 10px 0 30px 0;
    }

    .header {
      position: relative;
      height: 70px;
      line-height: 70px;
      padding: 0 20px;
      border-bottom: 1px solid #f5f5f5;

      h3 {
        font-weight: normal;
        font-size: 18px;
      }

      a {
        position: absolute;
        right: 25px;
        top: 25px;
        font-size: 24px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;

        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
